﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CAEXFileTypeViewModel.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   ViewModel representing an AutomationML File <see cref="CAEXFileType" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.ViewModels.CAEXObjectViewModels
{
    using System;
    using System.Globalization;
    using System.IO;

    using AMLEngineExtensions;

    using CAEX_ClassModel;

    using AutomationMLMapper.ViewModels.SystemViewModels;

    /// <summary>
    ///   ViewModel representing an AutomationML File <see cref="CAEXFileType" />
    /// </summary>
    public sealed class CAEXFileTypeViewModel : CAEXObjectViewModel<CAEXFileType>
    {
        #region Static Fields

        /// <summary>
        ///   The default meta data for a Caex File generated with this tool.
        /// </summary>
        private static readonly MetaInformation CaexFileDefaultMetaData;

        #endregion

        #region Fields

        /// <summary>
        ///   The caex document.
        /// </summary>
        private CAEXDocument caexDocument;

        /// <summary>
        ///   The file path.
        /// </summary>
        private string filePath = string.Empty;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes static members of the <see cref="CAEXFileTypeViewModel"/> class. 
        /// </summary>
        static CAEXFileTypeViewModel()
        {
            CaexFileDefaultMetaData = new MetaInformation
                {
                    WriterID = "AML21FB8FE3-E1E4-44E1-BD16-2D5320775CBE", 
                    WriterName = "AutomationMLMapper", 
                    WriterProjectID = "01", 
                    WriterProjectTitle = "AutomationMLMapping", 
                    WriterVendor = "AutomationML OpenSource Group", 
                    WriterVendorURL = string.Empty, 
                    WriterVersion = "01", 
                    WriterRelease = "01", 
                    LastWritingDateTime = DateTime.Now.ToString(CultureInfo.InvariantCulture)
                };
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CAEXFileTypeViewModel"/> class for an existing CaexFile.
        /// </summary>
        /// <param name="file">
        /// CAEX File 
        /// </param>
        public CAEXFileTypeViewModel(CAEXFileType file)
            : base(file)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CAEXFileTypeViewModel"/> class for an empty CAEXDocument. 
        /// </summary>
        public CAEXFileTypeViewModel()
        {
            this.CaexDocument = CAEXDocument.New_CAEXDocument();
            this.CaexObject.SetMetaInformation(CaexFileDefaultMetaData);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   DisplayName of the CAEX-Element in the UI used in Headers and Labels
        /// </summary>
        public static new string DisplayName
        {
            get
            {
                return "AutomationML File";
            }
        }

        /// <summary>
        ///   the AutomationML CAEX-Document
        /// </summary>
        public CAEXDocument CaexDocument
        {
            get
            {
                return this.caexDocument;
            }

            set
            {
                this.caexDocument = value;
                this.CaexObject = this.caexDocument.CAEXFile;

                if (this.CaexObject != null)
                    this.CaexObject.UpdateCAEXObjectHashListsFastWithoutValidation ();
            }
        }

        /// <summary>
        ///   Full FilePath of the CAEXFile
        /// </summary>
        public string FilePath
        {
            get
            {
                if (string.IsNullOrEmpty(this.filePath))
                {
                    if (this.CaexObject != null && this.CaexObject.FileName.Exists())
                    {
                        this.filePath = this.CaexObject.FileName.Value;
                    }
                }

                return this.filePath;
            }

            set
            {
                this.filePath = value;
                if (this.CaexObject != null)
                {
                    this.CaexObject.FileName.Value = this.Filename;
                    this.OnNotifyPropertyChanged("FilePath");
                    this.OnNotifyPropertyChanged("Filename");
                }
            }
        }

        /// <summary>
        ///   Filename of the CAEXFile
        /// </summary>
        public string Filename
        {
            get
            {
                return !string.IsNullOrEmpty(this.FilePath) ? Path.GetFileName(this.FilePath) : string.Empty;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Gets or sets the system view model.
        /// </summary>
        internal SystemViewModel SystemViewModel { get; set; }

        #endregion

        #region Methods

        /// <summary>
        ///  Save CAEXDocument and update the AutomationMLMapper MetaInformation for that file
        /// </summary>
        /// <param name="fullFilePath">
        ///  Full Filename
        /// </param>
        internal void SaveToFile(string fullFilePath)
        {
            this.FilePath = fullFilePath;
            CaexFileDefaultMetaData.LastWritingDateTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
            this.CaexObject.SetMetaInformation(CaexFileDefaultMetaData);
            this.CaexDocument.SaveToFile(fullFilePath, true);
        }

        #endregion
    }
}